package cfgs.gestudent.dao.anyacademic;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import cfgs.gestudent.dao.connexio.ConnexioDB;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

public class InserirAnyAcademic {
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();

	public boolean inserirAnyAcademic(String nom, String dataInici, String dataFi){
		boolean seguir = false;
		Connection connexio = ConnexioDB.getConnexio();
		Statement st = null;
		String insert = "INSERT INTO ANY_ACADEMIC VALUES (NULL, '"+nom+"', '"+dataInici+"', '"+dataFi+"')";
		
		try {
			st = connexio.createStatement();
			connexio.setAutoCommit(false);
			int i = st.executeUpdate(insert);
			
			if(i == 0){
				seguir=false;
				connexio.rollback();
			}else{
				seguir=true;
				connexio.commit();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					rb.getString("missatgeErrorStatement") + "\n " + e,
					rb.getString("titolErrorStatement"),
					JOptionPane.ERROR_MESSAGE);
		}finally {
			try {
				connexio.close();
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane
						.showMessageDialog(null,
								rb.getString("missatgeErrorTancarConnexio")
										+ "\n " + e,
								rb.getString("titolErrorTancarConnexio"),
								JOptionPane.ERROR_MESSAGE);
			}
		}
		return seguir;
	}
}
